#include "widget.h"
#include "ui_widget.h"
#include "cliect.h"
#include <QTimer>
#include "dht11.h"
#include "led_test.h"
#include "light_test.h"
#include <thread>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    setAttribute(Qt::WA_DeleteOnClose);

    setAttribute(Qt::WA_StyledBackground, true);
    this->setObjectName("widget");
    this->setStyleSheet("#widget{border-image: url(:/res/menu.bmp);}");

    QTimer *timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, [&]
    {
        static int hh, tt, ll;
        int h = 0, t = 0, l = 0;
        int ret = read_ht(h, t);
        l = read_light();
        if(ret > 0 && (hh!=h || tt!=t || l!=ll))
        {
            char buf[640*480*3+1] = {0};
            sprintf(buf, "@%d:%d\n", h, t);
            Package_t pack;
            pack.cmd = SINGLE;
            memcpy(pack.data, buf, strlen(buf));
            humidity = h;
            temperature = t;
            ll = l;
            ui->label_H->setText("湿度：" + QString::number(h) + "%");
            ui->label_T->setText("温度：" + QString::number(t) + "℃");
            ui->label_L->setText("光照：" + QString(l?"过强":"正常"));
            Cliect::get()->Send_Msg(pack);

            if(l)
                setBeep(1);
            else
                setBeep(0);

            checkWarn();
        }
    });
   timer->start(1000);


   ::init_beep();
   ::init_fan();
   ::init_light();
   ::light_init();

   connect(Cliect::get(), &Cliect::sigCmd, this, [&](QString data)
   {
       if(QString(data) == "L_ON")
       {
           setLed(1);
       }
       else if(QString(data) == "L_OFF")
       {
           setLed(0);
       }
       else if(QString(data) == "W_ON")
       {
           setFan(1);
           fal_manul = true;
       }
       else if(QString(data) == "W_OFF")
       {
           setFan(0);
           fal_manul = false;
       }
   });

   connect(ui->pushButton_1, &QPushButton::clicked, this, &Widget::slot_T);
   connect(ui->pushButton_2, &QPushButton::clicked, this, &Widget::slot_T);
   connect(ui->pushButton_5, &QPushButton::clicked, this, &Widget::slot_T);
   connect(ui->pushButton_6, &QPushButton::clicked, this, &Widget::slot_T);

   connect(ui->pushButton_3, &QPushButton::clicked, this, &Widget::slot_H);
   connect(ui->pushButton_4, &QPushButton::clicked, this, &Widget::slot_H);
   connect(ui->pushButton_7, &QPushButton::clicked, this, &Widget::slot_H);
   connect(ui->pushButton_8, &QPushButton::clicked, this, &Widget::slot_H);

   ui->label_T_MIN->setText(QString::number(temperatureMin));
   ui->label_T_MAX->setText(QString::number(temperatureMax));
   ui->label_H_MIN->setText(QString::number(humidityMin));
   ui->label_H_MAX->setText(QString::number(humidityMax));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::checkWarn()
{
    bool fan = false, water=false, ptc=false;
    if(temperature > temperatureMax)
    {
        setFan(1);
        water_onOff(1);

        fan = true;
        water = true;
    }
    else if(temperature < temperatureMin)
    {
        ptc_onOff(1);

        ptc = true;
    }

    if(humidity > humidityMax)
    {
        setFan(1);
        ptc_onOff(1);

        fan = true;
        ptc = true;
    }
    else if(humidity < humidityMin)
    {
        water_onOff(1);
        water = true;
    }

    if(!fan && !fal_manul)
        setFan(0);
    if(!ptc)
        ptc_onOff(0);
    if(!water)
        water_onOff(0);
}

void Widget::slot_H()
{
    QString name = sender()->objectName();
    if(name.contains("4"))
        humidityMin--;
    else if(name.contains("3"))
        humidityMin++;
    else if(name.contains("7"))
        humidityMax++;
    else if(name.contains("8"))
        humidityMax--;

    ui->label_H_MIN->setText(QString::number(humidityMin));
    ui->label_H_MAX->setText(QString::number(humidityMax));

    checkWarn();
}

void Widget::slot_T()
{
    QString name = sender()->objectName();
    if(name.contains("1"))
        temperatureMin--;
    else if(name.contains("2"))
        temperatureMin++;
    else if(name.contains("5"))
        temperatureMax++;
    else if(name.contains("6"))
        temperatureMax--;

    ui->label_T_MIN->setText(QString::number(temperatureMin));
    ui->label_T_MAX->setText(QString::number(temperatureMax));

    checkWarn();
}

void Widget::on_pushButton_fan_clicked()
{
    setFan(!fan_onOff);
    fal_manul = fan_onOff;
}

void Widget::on_pushButton_voice_clicked()
{
    system("./record 1.wav");

    Cliect::get()->send_file("1.wav", ".");
}

void Widget::on_pushButton_led_clicked()
{
    setLed(!led_onOff);
}

void Widget::setLed(bool on)
{
    if(on)
    {
        ::led_onOff(1);
    }
    else
    {
        ::led_onOff(0);
    }
    led_onOff = on;
}

void Widget::setBeep(bool on)
{
    if(on)
    {
        ::beep_onOff(1);
    }
    else
    {
        ::beep_onOff(0);
    }
    beep_onOff = on;
}

void Widget::setFan(bool on)
{
    if(on)
    {
        ::fan_onOff(1);
    }
    else
    {
        ::fan_onOff(0);
    }
    fan_onOff = on;
}
